package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

public class BlogLadyBlogAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://www.bloglady.cn/lady[0-9]*/.*/archives/[0-9]{4}/[0-9]+.html",
				"http://fashion.bloglady.cn/[a-z]*/[a-z]*/[0-9]*/[0-9]*/[0-9]*.shtml"
				};
		//http://fashion.bloglady.cn/fashion/popinfo/2015/0305/4358680981208.shtml
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if(!isDetailPage(url)){
		}
		String htmltxt = urlMeta.getHtml();
		Document doc = Jsoup.parse(htmltxt);
		NewsMeta blog = new NewsMeta();
		String title = "";
		String content = null;
		Long date = null;
		Integer commentNum = 0;
		Integer clickNum = 0;
		Integer deliverNum = 0;
		if(doc.select("span.style12").size()>1){
			title = doc.select("span.style12").get(0).text();
		}
		if(StringUtils.isBlank(title)){
			 Element first = doc.select("div#main div#innermain div#mainleft div.content span.body-itemmessage table tbody tr td table tbody tr td strong").first();
			 if(first!=null){
				 title= first.text();
			 }
		}
		title=doc.select("h1").text();
		if (title == null || title.trim().length() == 0) {
			title = doc.select("strong").text();
			if (title == null || title.trim().length() == 0) {
				title = doc
						.select("html body div div#body_div div#Container div.PageBody div.MainBody div.topic")
						.text();
				if (title == null || title.trim().length() == 0) {
					title = doc
							.select("html body table tbody tr td table tbody tr td table tbody tr td.b_title")
							.text();
					if (title == null || title.trim().length() == 0) {
						if(doc.select("span.rtitle").size()>1){
							title = doc
									.select("span.rtitle").get(0)
									.text();
						}
						if (title == null || title.trim().length() == 0) {
							title = doc
									.select("html body table.maxbox tbody tr td table.maxbox2 tbody tr td table tbody tr td.leftframe table tbody tr td.leftmain table tbody tr td.entry table tbody tr td span.arttitle")
									.text();
							if (title == null || title.trim().length() == 0) {
								title = doc
										.select("html body div#wrapper div#innerWrapper div#mainWrapper div#content.content div#innerContent div.article-bottom div.pages table tbody tr td.Ptitle")
										.text();
								if (title == null || title.trim().length() == 0) {
								}
							}
						}
					}
				}
			}
		}
		/**
		 * 时间
		 */
		
		date = DateUtils.matchDate(doc.select("html body table tbody tr td table tbody tr td table tbody tr td table tbody tr td div").text());
		if(date == null){
			date = DateUtils.matchDate(doc.select("span.textbox-label").text());

			if(date == null){
				date = DateUtils.matchDate(doc.select("html body table tbody tr td table tbody tr td table tbody tr td.b_date").text());
				if(date == null){
					date = DateUtils.matchDate(doc.select("html body div div#body_div div#Container div.PageBody div.MainBody div.loginfo").text());
					if(date == null){
						date = DateUtils.matchDate(doc.select("table[style=TABLE-LAYOUT: fixed; WORD-BREAK: break-all]").text());
						if(date == null){
							date = DateUtils.matchDate(doc.select("html body div#main div#right div#diary_tag table tbody tr td table tbody tr td div").text());
							if(date == null){
								date = DateUtils.matchDate(doc.select("html body table.maxbox tbody tr td table.maxbox2 tbody tr td table tbody tr td.leftframe table tbody tr td.leftmain table tbody tr td.entry table tbody tr td.details").text());
								if(date == null){
									date = DateUtils.matchDate(doc.select("html body div#wrapper div#innerWrapper div#mainWrapper div#content.content div#innerContent div.article-bottom div.pages table tbody tr td").text());
									if(date == null){
										date = DateUtils.matchDate(doc.select("td#com").text());
										if(date == null){
											date = DateUtils.matchDate(doc.select("div:contains( | By:").text());
										}
									}
								}
							}
						}
					}
				}
			}
		}
		date=DateUtils.matchDate(doc.select("dl.viewtis dd").text());
		/**
		 * 内容
		 */
		
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.innertext"));
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select("span.oblog_text"));
			if(StringUtils.isBlank(content)){
				content = HtmlCleaner.getContentHtml(url,  doc.select("#Container div.PageBody div.MainBody div.logtext .logtext_table"));
				if(StringUtils.isBlank(content)){
					try {
						content = HtmlCleaner.getContentHtml(url,  doc.select("table[style=table-layout:fixed;]").first());
						if(StringUtils.isBlank(content)){
							content = HtmlCleaner.getContentHtml(url,  doc.select("tr[bgcolor=#ffffff]").first());
							if(StringUtils.isBlank(content)){
								content = HtmlCleaner.getContentHtml(url,  doc.select("div#content td.oblog_text"));
								if(StringUtils.isBlank(content)){
									content = HtmlCleaner.getContentHtml(url,  doc.select("div#diary_tag table tbody tr td table tbody tr td div"));
									if(StringUtils.isBlank(content)){
										if(doc.select("td.leftmain table tbody tr td.entry table tbody tr").size()>2){
											content = HtmlCleaner.getContentHtml(url,  doc.select("td.leftmain table tbody tr td.entry table tbody tr").get(2));
										}
										if(StringUtils.isBlank(content)){
											content = HtmlCleaner.getContentHtml(url,  doc.select("div#innerContent div.article-bottom div.pages table tbody tr td.Pcontent"));
											if(StringUtils.isBlank(content)){
												content = HtmlCleaner.getContentHtml(url,  doc.select("td[valign=top] p"));
											}
										}
									}
								}
							}
						}
					} catch (Exception e) {
					}
				}
			}
		}

		content=doc.select("div.viewbd").html();
		String author = doc.select("meta[name=author]").attr("content");
		String[] splits  = author.split(",");
		if(splits.length==2){
			author = splits[1];
		}else{
			if(author.endsWith(",")){
				author = author.substring(0,author.length()-1);
			}
		}

		/**
		 * 回复数
		 */
		String comment = doc.select("a[href=#cmt]").text();
		if(!StringUtils.isBlank(comment)){
			comment = comment.replace("回复(", "").replace(")", "");
			try {
				commentNum = Integer.parseInt(comment);
			} catch (NumberFormatException e) {
				commentNum = 0;
			}
		}
		/**
		 * 更新阅读数
		 */
		String updateId = null;
		updateId = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
		if (updateId != null) {
			String updateUrl = "http://www.bloglady.cn/count.asp?action=logtb31&id="+updateId;
			UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
			String responseStr = responseToURL.getHtml();
			if (responseStr != null) {
				Pattern reg = Pattern.compile("document.getElementById[(]'ob_logreaded'[)].innerHTML='[(][0-9]+[)]';");
				Matcher matcher = reg.matcher(responseStr);
				if (matcher.find()) {
					String clicknumStr = matcher.group();
					clicknumStr = clicknumStr.replace("document.getElementById('ob_logreaded').innerHTML='(", "").replace(")';", "");
					/**
					 * 阅读数
					 */
					int clicknum = Integer.parseInt(clicknumStr);
					blog.setClickNum(clicknum);
				}
			}
		}
		blog.setUpdateUrl(url);
		if(StringUtils.isBlank(title) || date==null){
		}
		blog.setUrl(url);
		blog.setTitle(title);
		blog.setContent(content);
		blog.setDate(date);
		blog.setAuthor(author);
		blog.setCommentNum(commentNum);
		//blog.setDeliverNum(deliverNum);
		blog.setType(7);
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String updateUrl = meta.getUpdateUrl();
		UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
		NewsMeta blogMeta = parserHtml(responseToURL);
		return blogMeta;
	}

	/**
	 * @param args
	 */
	

	
}
